package net.hn.hnms.biz.intelligence.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import net.hn.hnms.biz.intelligence.service.SafetyIntelligenceService;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.biz.intelligence.vo.*;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("intelligence")
@Api(value = "安全决策", tags = {"安全决策"})
@RequiredArgsConstructor
public class SafetyIntelligenceController {

    private final SafetyIntelligenceService safetyIntelligenceService;

    @ApiOperation("【智能分析】公司端-瓦斯异常分析")
    @GetMapping("/gas/collectGasAlarm")
    public R<ResponsePage<CollectGasAlarmVO>> collectGasAlarm(@RequestParam(defaultValue = "0") int pageNum, @RequestParam(defaultValue = "20") int pageSize,
                                                              String mineCode, @NotNull @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                                              @NotNull @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
                                                              String outlierType){
        if (DateUtil.compare(startTime, endTime) < 0) {
            return R.ok(safetyIntelligenceService.collectGasAlarm(
                    pageNum, pageSize, mineCode, startTime, endTime, outlierType));
        } else {
            return R.fail("开始日期大于等于结束日期");
        }
    }


    @ApiOperation(value = "【智能分析】公司端-瓦斯异常分析-单个测点值变化曲线")
    @GetMapping(value = "/gas/sensorValue")
    public R<Map<String, Object>> sensorValue(@NotBlank String pointCode,
                                 @NotNull @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                 @NotNull @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
                                      @RequestParam String outlierType) {
//
        if (DateUtil.between(startTime, endTime, DateUnit.DAY) > 1) {
            return R.fail("请查询1天内的数据");
        } else {
            if (DateUtil.compare(startTime, endTime) < 0) {
                return R.ok(safetyIntelligenceService.querySensorValue(pointCode,
                                startTime, endTime, outlierType));
            } else {
                return R.fail("开始时间大于等于结束时间");
            }
        }

    }

    @ApiOperation("【智能分析】公司端-疑似睡岗")
    @GetMapping(value = "/collectSuspectedSleepDuty")
    public R<ResponsePage<CollectSuspectedSleepDutyVO>> collectSuspectedSleepDuty(@RequestParam(defaultValue = "0") int pageNum,
                                                                                  @RequestParam(defaultValue = "20") int pageSize,
                                                                                  String mineCode,
                                                                                  @NotNull @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                                                                  @NotNull @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
                                                                                  @RequestParam(defaultValue = "4") Double continueTimeLength) {

        if (DateUtil.compare(startTime, endTime) < 0) {
            return R.ok(safetyIntelligenceService.collectSuspectedSleepDuty(
                    pageNum, pageSize, mineCode, startTime, endTime, continueTimeLength));
        } else {
            return R.fail("开始日期大于等于结束日期");
        }

    }

    @ApiOperation("【智能分析】公司端-疑似睡岗-人员轨迹")
    @GetMapping(value = "/personTrace")
    public R<List<PersonTraceVO>> personTrace(String mineCode, @NotBlank String staffCode,
                                 @NotNull @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date wellTime,
                                @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date wellOutTime) {

        if (DateUtil.compare(wellTime, wellOutTime) <= 0 ||
                (null == wellOutTime && DateUtil.compare(wellTime, new Date()) <= 0)) {
            wellOutTime = DateUtils.addMinutes(wellTime, 30);
            return R.ok(safetyIntelligenceService.personTrace(mineCode, staffCode, wellTime, wellOutTime));
        } else {
            return R.fail("开始时间大于等于结束时间");
        }
    }

    @ApiOperation("【智能分析】公司端-水量分析")
    @GetMapping(value = "/collectHydrogeoAmountAlarmStatistics")
    public R<ResponsePage<HydrogeoAmountAlarmStatisticsVO>> collectHydrogeoAmountAlarmStatistics(@RequestParam(defaultValue = "0") int pageNum,
                                                                                                 @RequestParam(defaultValue = "20") int pageSize,
                                                                                                 String mineCode,
                                                                                                 @NotNull @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                                                                                 @NotNull @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
                                                                                                 String alarmType, String handleFlag) {
        if (DateUtil.compare(startTime, endTime) < 0) {
            return R.ok(safetyIntelligenceService.collectHydrogeoAmountAlarmStatistics(
                    pageNum, pageSize, mineCode, startTime, endTime, alarmType, handleFlag));
        } else {
            return R.fail("开始日期大于等于结束日期");
        }
    }

    @ApiOperation("【智能分析】公司端-井下人员曲线")
    @GetMapping(value = "/chartSumPersonCountForMineCode")
    public R<Map<String, Object>> chartSumPersonCountForMineCode(
            String mineCode,
            @NotNull @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
            @NotNull @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        long second = DateUtil.between(startTime, endTime, DateUnit.SECOND);
        if(second > 24 * 60 * 60) {
            //throw new BadRequestException("查询时间范围不能超过24个小时");
            return R.fail("查询时间范围不能超过24个小时");
        }
        if (DateUtil.compare(startTime, endTime) < 0) {
            return R.ok(safetyIntelligenceService.chartSumPersonCountForMineCode(mineCode, startTime, endTime));
        } else {
            return R.fail("开始日期大于等于结束日期");
        }
    }

    @ApiOperation("【智能分析】公司端-出入井人数曲线")
    @GetMapping(value = "/chartStatisticsInOutStaff")
    public R<Map<String, Object>> chartStatisticsInOutStaff(
            String mineCode,
            @NotNull @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
            @NotNull @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        long second = DateUtil.between(startTime, endTime, DateUnit.SECOND);
        if(second > 24 * 60 * 60) {
            //throw new BadRequestException("查询时间范围不能超过24个小时");
            return R.fail("查询时间范围不能超过24个小时");
        }
        if (DateUtil.compare(startTime, endTime) < 0) {

            return R.ok(safetyIntelligenceService.chartStatisticsInOutStaff(mineCode, startTime, endTime));
        } else {
            return R.fail("开始时间大于等于结束时间");
        }
    }

    @ApiOperation("【智能分析】公司端-出井异常分析")
    @GetMapping(value = "/collectPersonWellOutAbnormalCount")
    public R<ResponsePage<CollectPersonWellOutAbnormalCountVO>> collectPersonWellOutAbnormalCount(
            @RequestParam(defaultValue = "0") int pageNum,
            @RequestParam(defaultValue = "20") int pageSize,
            String mineCode,
            @NotNull @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
            @NotNull @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
            @Min(10) String lowerLimit) {

        if (DateUtil.compare(startTime, endTime) < 0) {

            return R.ok(
                    safetyIntelligenceService.collectPersonWellOutAbnormalCount(pageNum, pageSize, mineCode, startTime, endTime, lowerLimit));
        } else {
            return R.fail("开始时间大于等于结束时间");
        }
    }

    @ApiOperation("【智能分析】公司端-出井异常分析 - 人员出井异常明细")
    @GetMapping(value = "/collectPersonWellOutAbnormalDetailed")
    public R<List<PersonHistoryVO>> collectPersonWellOutAbnormalDetailed(String mineCode,
                                                                                 @NotNull @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date wellOutTime) {

        return R.ok(
                safetyIntelligenceService.collectPersonWellOutAbnormalDetailed(mineCode, wellOutTime));
    }


    @ApiOperation("【智能分析】公司端-带班空岗分析 - 各矿带班空岗情况统计表")
    @GetMapping("/collectMineLeaderBlankStatistics")
    public R<List<CollectMineLeaderBlankVO>> collectMineLeaderBlankStatistics(String mineCode,
                                                                              @NotNull @RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") String startTime,
                                                                              @NotNull @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") String endTime) {
        return R.ok(
                safetyIntelligenceService.collectMineLeaderBlankStatistics(mineCode, startTime, endTime));
    }

    @ApiOperation("【智能分析】公司端-带班空岗分析 - 各矿带班空岗情况明细表")
    @GetMapping("/collectMineLeaderDetails")
    public R<List<CollectMineLeaderDetailVO>> collectMineLeaderDetails(String mineCode,
                                                                               @RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") String startTime,
                                                                               @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") String endTime) {

        return R.ok(
                safetyIntelligenceService.collectMineLeaderDetail(mineCode, startTime, endTime));
    }

    @ApiOperation("【智能分析】公司端-带班空岗分析 - 带班空岗煤矿数与总时长曲线 - 按天")
    @GetMapping("/chartSumLeaderBlank")
    public R<Map<String, Object>> chartSumLeaderBlank(String mineCode,
                                                      @NotNull @RequestParam("startDate")  @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") String startDate,
                                                      @NotNull @RequestParam("endDate")  @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") String endDate) {
        return R.ok(safetyIntelligenceService.chartSumLeaderBlank(mineCode, startDate, endDate));
    }
}
